﻿using System.Windows.Forms;
using Finance.Properties;
using Finance.ViewModels;

namespace Finance.Views
{
	internal partial class RepayDebtDocumentControl : UserControl
	{
		public RepayDebtDocumentControl()
		{
			InitializeComponent();
			_debtDocumentComboBox.DataSource = new BindingSource();

			_rateAmountLabel.Text = Resources.RepayDebtDocumentControl_InterestPaymentAmountLabel;
			_debtAmountLabel.Text = Resources.RepayDebtDocumentControl_DebtPaymentAmount;
			label1.Text = Resources.RepayDebtDocumentControl_CommentsLabelName;
			_rateLabel.Text = Resources.RepayDebtDocumentControl_DebtDocument;
			label2.Text = Resources.RepayDebtDocumentControl_CalculatedRemain;
		}

		private RepayDebtDocumentViewModel _viewModel;

		public RepayDebtDocumentViewModel ViewModel
		{
			set
			{
				_documentBaseControl.ViewModel = value;

				if (value == null || value == _viewModel)
					return;

                _viewModel = value;
                
				_textBoxDescription.DataBindings.Add(
					new Binding("Text", _viewModel, "Description", true, DataSourceUpdateMode.OnValidation));

				//Contractor
				var debtDocumentsBinding = (BindingSource)_debtDocumentComboBox.DataSource;
				debtDocumentsBinding.DataSource = _viewModel.DebtDocuments;
				var debtDocumentBinding = new Binding("SelectedItem", _viewModel, "SelectedDebtDocument", true);
				_debtDocumentComboBox.DataBindings.Add(debtDocumentBinding);

				_debtAmountTextBox.DataBindings.Add(
					new Binding("Text", _viewModel, "DebtAmount", true, DataSourceUpdateMode.OnValidation));
				
				_rateAmountTextBox.DataBindings.Add(
					new Binding("Text", _viewModel, "RateAmount", true, DataSourceUpdateMode.OnValidation));

				_calculatedRemainTextBox.DataBindings.Add(
					new Binding("Text", _viewModel, "DebtRemain", true, DataSourceUpdateMode.OnValidation));
			}
		}

		public bool Apply()
		{
			return _documentBaseControl.Apply();
		}
	}
}
